package com.example.mobile2121606110syc0331homework3;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.util.Arrays;
public class MainActivity extends AppCompatActivity {
    Button btn_0,btn_1,btn_2,btn_3,btn_4,btn_5,btn_6,btn_7,btn_8,btn_9,btn_pt;
    Button btn_mul,btn_div,btn_add,btn_sub;
    Button btn_clr,btn_del,btn_eq;
    EditText et_input;
    boolean clr_flag;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_0= (Button) findViewById(R.id.button17);
        btn_1= (Button) findViewById(R.id.button13);
        btn_2= (Button) findViewById(R.id.button14);
        btn_3= (Button) findViewById(R.id.button15);
        btn_4= (Button) findViewById(R.id.button9);
        btn_5= (Button) findViewById(R.id.button10);
        btn_6= (Button) findViewById(R.id.button11);
        btn_7= (Button) findViewById(R.id.button5);
        btn_8= (Button) findViewById(R.id.button6);
        btn_9= (Button) findViewById(R.id.button7);
        btn_pt= (Button) findViewById(R.id.button18);
        btn_add= (Button) findViewById(R.id.button16);
        btn_sub= (Button) findViewById(R.id.button8);
        btn_mul= (Button) findViewById(R.id.button2);
        btn_div= (Button) findViewById(R.id.button3);
        btn_clr= (Button) findViewById(R.id.button);
        btn_del= (Button) findViewById(R.id.button4);
        btn_eq= (Button) findViewById(R.id.button19);
        et_input= (EditText) findViewById(R.id.editTextTextPersonName);
        for (Button button : Arrays.asList(btn_0, btn_1, btn_2, btn_3, btn_4, btn_5, btn_6, btn_7, btn_8, btn_9, btn_pt, btn_add, btn_sub, btn_mul, btn_div, btn_clr, btn_del, btn_eq)) {
            button.setOnClickListener(this::OnClick);
    }
}
    public void OnClick(View v) {
        String str=et_input.getText().toString();
        switch (v.getId()){
            case   R.id.button17:
            case   R.id.button13:
            case   R.id.button14:
            case   R.id.button15:
            case   R.id.button9:
            case   R.id.button10:
            case   R.id.button11:
            case   R.id.button5:
            case   R.id.button6:
            case   R.id.button7:
            case   R.id.button18:
                if(clr_flag){
                    clr_flag=false;
                    str="";
                    et_input.setText("");
                }
                et_input.setText(str+((Button)v).getText());
                break;
            case R.id.button16:
            case R.id.button8:
            case R.id.button2:
            case R.id.button3:
                if(clr_flag){
                    clr_flag=false;
                    str="";
                    et_input.setText("");
                }
                if(str.contains("+")||str.contains("-")||str.contains("*")||str.contains("÷")) {
                    str=str.substring(0,str.indexOf(" "));
                }
                et_input.setText(str+" "+((Button)v).getText()+" ");
                break;
            case R.id.button:
                if(clr_flag)
                    clr_flag=false;
                str="";
                et_input.setText("");
                break;
            case R.id.button4:
                if(clr_flag){
                    clr_flag=false;
                    str="";
                    et_input.setText("");
                }
                else if(str!=null&&!str.equals("")){
                    et_input.setText(str.substring(0,str.length()-1));
                }
                break;
            case R.id.button19:
                getResult();
                break;
        }
    }

    private void getResult() {
        String exp=et_input.getText().toString();
        if(exp==null||exp.equals("")) return ;
        if(!exp.contains(" ")){
            return ;
        }
        if(clr_flag){
            clr_flag=false;
            return;
        }
        clr_flag=true;
        String s1=exp.substring(0,exp.indexOf(" "));
        String op=exp.substring(exp.indexOf(" ")+1,exp.indexOf(" ")+2);
        String s2=exp.substring(exp.indexOf(" ")+3);
        double cnt=0;
        if(!s1.equals("")&&!s2.equals("")){
            double d1=Double.parseDouble(s1);
            double d2=Double.parseDouble(s2);
            if(op.equals("+")){
                cnt=d1+d2;
            }
            if(op.equals("-")){
                cnt=d1-d2;
            }
            if(op.equals("×")){
                cnt=d1*d2;
            }
            if(op.equals("÷")){
                if(d2==0) cnt=0;
                else cnt=d1/d2;
            }
            if(!s1.contains(".")&&!s2.contains(".")&&!op.equals("÷")) {
                int res = (int) cnt;
                et_input.setText(res+"");
            }else {
                et_input.setText(cnt+"");}
        }
        else if(!s1.equals("")&&s2.equals("")){
            double d1=Double.parseDouble(s1);
            if(op.equals("+")){
                cnt=d1;
            }
            if(op.equals("-")){
                cnt=d1;
            }
            if(op.equals("×")){
                cnt=0;
            }
            if(op.equals("÷")){
                cnt=0;
            }
            if(!s1.contains(".")) {
                int res = (int) cnt;
                et_input.setText(res+"");
            }else {
                et_input.setText(cnt+"");}
        }
        else if(s1.equals("")&&!s2.equals("")){
            double d2=Double.parseDouble(s2);
            if(op.equals("+")){
                cnt=d2;
            }
            if(op.equals("-")){
                cnt=0-d2;
            }
            if(op.equals("×")){
                cnt=0;
            }
            if(op.equals("÷")){
                cnt=0;
            }
            if(!s2.contains(".")) {
                int res = (int) cnt;
                et_input.setText(res+"");
            }else {
                et_input.setText(cnt+"");}
        }
        else {
            et_input.setText("");
        }
    }

}